package com.weirong.smartvault.backservice.util.statistics;

import com.weirong.smartvault.common.tools.spring.context.SpringContextUtil;
import com.weirong.smartvault.backservice.util.encry.MD5EncryUtil;
import com.weirong.smartvault.dao.StatisticsDataRepository;
import com.weirong.smartvault.dao.StockHistoryRepository;
import com.weirong.smartvault.entity.StatisticsData;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Date;

/**
 * 于2017/12/22由 KFEB4 创建 .
 */
public enum StatisticsType {
    STOCKBYMONTH("库存月统计量", 1,StatisticsCalculation.stockHistory.class,StatisticsCheckAvailable.stockHistory.class,String.class, Integer.class,Integer.class),//库存的月统计量String currencyType, Integer year,Integer month
    STOCKBYDAT("库存日统计量",2,StatisticsCalculation.stockHistory.class,StatisticsCheckAvailable.stockHistory.class,String.class,Integer.class,Integer.class,Integer.class),
    USEMONTH("使用月统计量",3,StatisticsCalculation.use.class,StatisticsCheckAvailable.use.class,String.class, Integer.class,Integer.class),
    USEDAY("使用日统计量",4,StatisticsCalculation.use.class,StatisticsCheckAvailable.use.class,String.class, Integer.class,Integer.class,Integer.class);

    private String name;
    private Integer value;
    private Class[] args;
    private Class goalClass;
    private Class checkClass;
    private static StatisticsDataRepository statisticsDataRepository = SpringContextUtil.getBean(StatisticsDataRepository.class);
    private static StockHistoryRepository stockHistoryRepository = SpringContextUtil.getBean(StockHistoryRepository.class);

    StatisticsType(String name, Integer value,Class goalClass,Class checkClass,Class... args){
        this.name = name;
        this.value = value;
        this.args = args;
        this.goalClass = goalClass;
        this.checkClass = checkClass;
    }
    public double cal(Object ... args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        String key = Protocol.trans(args);
        key = this.name+","+key;
        String code = MD5EncryUtil.encodeByMD5(key);
        StatisticsData statisticsData = statisticsDataRepository.findOne(code);
        if (statisticsData != null){
            //检查是否有效
            Class[] cargs = new Class[this.args.length+1];
            cargs[0] = StatisticsData.class;
            System.arraycopy(this.args, 0, cargs, 1, cargs.length - 1);
            Method method = checkClass.getMethod(StatisticsCheckAvailable.FunctionName,cargs);
            Object[] pargs = new Object[args.length+1];
            pargs[0] = statisticsData;
            System.arraycopy(args, 0, pargs, 1, cargs.length - 1);
            if ((Boolean) method.invoke(null,pargs))
                return statisticsDataRepository.getOne(code).getVal();
        }
        Method cal = goalClass.getMethod(StatisticsCalculation.funcionName,this.args);
        double res = (double) cal.invoke(null,args);
        StatisticsData statData = new StatisticsData();
        statData.setCode(code);
        statData.setModifyTime(new Date());
        statData.setVal(res);
        //TODO: 设置校验数据
        statisticsDataRepository.save(statData);
        return res;
    }
    public static StatisticsType getByValue(Integer value){
        for(StatisticsType t:StatisticsType.values()){
            if (t.value==value)
                return t;
        }
        return null;
    }
    public Class[] getArgs(){
        return args;
    }
    @Override
    public String toString() {
        return value.toString();
    }
}
